﻿using System;
using UnityEngine;

// Token: 0x020002D9 RID: 729
public class JiggleBone : MonoBehaviour
{
	// Token: 0x1700026C RID: 620
	// (get) Token: 0x060010F8 RID: 4344 RVA: 0x0007D51E File Offset: 0x0007B71E
	// (set) Token: 0x060010F9 RID: 4345 RVA: 0x0007D52B File Offset: 0x0007B72B
	private Vector3 dynamicPos
	{
		get
		{
			return this.virtualDynamicPos.ToWorld();
		}
		set
		{
			this.virtualDynamicPos = value.ToVirtual();
		}
	}

	// Token: 0x1700026D RID: 621
	// (get) Token: 0x060010FA RID: 4346 RVA: 0x0007D53C File Offset: 0x0007B73C
	public float targetDistance
	{
		get
		{
			return this.unscaledTargetDistance * base.transform.lossyScale.y;
		}
	}

	// Token: 0x060010FB RID: 4347 RVA: 0x0007D564 File Offset: 0x0007B764
	private void Awake()
	{
		Vector3 dynamicPos = base.transform.position + base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance));
		this.dynamicPos = dynamicPos;
	}

	// Token: 0x060010FC RID: 4348 RVA: 0x0007D5D0 File Offset: 0x0007B7D0
	private void LateUpdate()
	{
		base.transform.rotation = default(Quaternion);
		Vector3 dir = base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance));
		Vector3 vector = base.transform.TransformDirection(new Vector3(0f, 1f, 0f));
		Vector3 vector2 = base.transform.position + base.transform.TransformDirection(new Vector3(this.boneAxis.x * this.targetDistance, this.boneAxis.y * this.targetDistance, this.boneAxis.z * this.targetDistance));
		this.force.x = (vector2.x - this.dynamicPos.x) * this.bStiffness;
		this.acc.x = this.force.x / this.bMass;
		this.vel.x = this.vel.x + this.acc.x * (1f - this.bDamping);
		this.force.y = (vector2.y - this.dynamicPos.y) * this.bStiffness;
		this.force.y = this.force.y - this.bGravity / 10f;
		this.acc.y = this.force.y / this.bMass;
		this.vel.y = this.vel.y + this.acc.y * (1f - this.bDamping);
		this.force.z = (vector2.z - this.dynamicPos.z) * this.bStiffness;
		this.acc.z = this.force.z / this.bMass;
		this.vel.z = this.vel.z + this.acc.z * (1f - this.bDamping);
		this.dynamicPos += this.vel + this.force;
		base.transform.LookAt(this.dynamicPos, vector);
		if (this.SquashAndStretch)
		{
			float magnitude = (this.dynamicPos - vector2).magnitude;
			float x;
			if (this.boneAxis.x == 0f)
			{
				x = 1f + -magnitude * this.sideStretch;
			}
			else
			{
				x = 1f + magnitude * this.frontStretch;
			}
			float y;
			if (this.boneAxis.y == 0f)
			{
				y = 1f + -magnitude * this.sideStretch;
			}
			else
			{
				y = 1f + magnitude * this.frontStretch;
			}
			float z;
			if (this.boneAxis.z == 0f)
			{
				z = 1f + -magnitude * this.sideStretch;
			}
			else
			{
				z = 1f + magnitude * this.frontStretch;
			}
			base.transform.localScale = new Vector3(x, y, z);
		}
		if (this.debugMode)
		{
			Debug.DrawRay(base.transform.position, dir, Color.blue);
			Debug.DrawRay(base.transform.position, vector, Color.green);
			Debug.DrawRay(vector2, Vector3.up * 0.2f, Color.yellow);
			Debug.DrawRay(this.dynamicPos, Vector3.up * 0.2f, Color.red);
		}
	}

	// Token: 0x0400162D RID: 5677
	public bool debugMode;

	// Token: 0x0400162E RID: 5678
	private Vector3 virtualDynamicPos = default(Vector3);

	// Token: 0x0400162F RID: 5679
	public Vector3 boneAxis = new Vector3(0f, 0f, 1f);

	// Token: 0x04001630 RID: 5680
	public float unscaledTargetDistance = 2000f;

	// Token: 0x04001631 RID: 5681
	public float bStiffness = 0.1f;

	// Token: 0x04001632 RID: 5682
	public float bMass = 2f;

	// Token: 0x04001633 RID: 5683
	public float bDamping = 0.2f;

	// Token: 0x04001634 RID: 5684
	public float bGravity = 0.2f;

	// Token: 0x04001635 RID: 5685
	private Vector3 force = default(Vector3);

	// Token: 0x04001636 RID: 5686
	private Vector3 acc = default(Vector3);

	// Token: 0x04001637 RID: 5687
	private Vector3 vel = default(Vector3);

	// Token: 0x04001638 RID: 5688
	public bool SquashAndStretch;

	// Token: 0x04001639 RID: 5689
	public float sideStretch = 0.15f;

	// Token: 0x0400163A RID: 5690
	public float frontStretch = 0.2f;
}
